package com.diandian.backend.log.service;

import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.log.domain.LogTaskProcess;
import com.diandian.backend.log.dto.*;
import com.diandian.backend.log.mapper.LogTaskProcessMapper;
import com.diandian.backend.log.resp.LogTaskProcessPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.log.resp.LogTaskProcessListResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LogTaskProcessService {

    @Autowired
    private LogTaskProcessMapper logTaskProcessMapper;

    public PageResult page(LogTaskProcessPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = logTaskProcessMapper.getCount(requestDTO);

        // 查询数据集合
        List<LogTaskProcess> dataList = logTaskProcessMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<LogTaskProcessPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                LogTaskProcessPageResp item = new LogTaskProcessPageResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setBusinessType(x.getBusinessType());
                item.setBusinessId(x.getBusinessId());
                item.setTaskId(x.getTaskId());
                item.setTaskName(x.getTaskName());
                item.setContent(x.getContent());
                item.setOperaterUserId(x.getOperaterUserId());
                item.setOperatorUserName(x.getOperatorUserName());
                item.setCreateTime(x.getCreateTime());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<LogTaskProcessListResp> getList(LogTaskProcessListParamDTO requestDTO){

        requestDTO.check();

        // 查询数据集合
        List<LogTaskProcess> dataList = logTaskProcessMapper.getList(requestDTO);

        // 组装数据
        List<LogTaskProcessListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                LogTaskProcessListResp item = new LogTaskProcessListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setBusinessType(x.getBusinessType());
                item.setBusinessId(x.getBusinessId());
                item.setTaskId(x.getTaskId());
                item.setTaskName(x.getTaskName());
                item.setContent(x.getContent());
                item.setOperaterUserId(x.getOperaterUserId());
                item.setOperatorUserName(x.getOperatorUserName());
                item.setCreateTime(x.getCreateTime());
                respList.add(item);
            });
        }

        return respList;
    }

    public void add(LogTaskProcessAddParamDTO param) {
        param.setDel(0);
        param.setCreateTime(new Date());
        logTaskProcessMapper.add(param);
    }
}
